package cn.wsh.org.controller;

import cn.wsh.MyLog;
import cn.wsh.basic.util. AjaxResult;
import cn.wsh.basic.util.PageList;
import cn.wsh.org.domain.Department;
import cn.wsh.org.domain.Detail;
import cn.wsh.org.query.DepartmentQuery;
import cn.wsh.org.service.IDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController// = @Controller + @ResponseBody
@RequestMapping("/detail")//
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class DetailController {
    @Autowired
    private IDetailService detailService;

    @Test
    public void queryAllTest() {
        detailService.queryAll().forEach(System.out::println);
    }

    @MyLog(value = "新增和修改")
    //新增和修改   put
    @PutMapping// http://localhost:8080/dept/2
    @ApiOperation(value = "新增或修改，如果传递ID=修改，否则新增")
    public  AjaxResult addOrUpdate(@RequestBody Detail department) {
        try {
            if (department.getId() == null) {
                detailService.add(department);
            } else {
                detailService.update(department);
            }
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统报错了，我们正在殴打程序员！！");
        }
    }

    @MyLog(value = "删除")
    // 删除      delete
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")// http://localhost:8080/dept/2
    public  AjaxResult remove(@PathVariable("id") Long id) {
        try {
            detailService.remove(id);
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统报错了！！");
        }
    }

    @MyLog(value = "报错")
    @PatchMapping
    public  AjaxResult batch(@RequestBody List<Long> ids) {
        try {
            detailService.batch(ids);
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统报错了，我们正在殴打程序员！！");
        }
    }


    @MyLog(value = "获取一条记录")
    //获取一条记录  get  /1
    @ApiOperation(value = "查询单条数据")
    @GetMapping("/{id}")// http://localhost:8080/dept/2
    public Detail queryById(@PathVariable("id") Long id) {
        return detailService.queryById(id);
    }

    @MyLog(value = "获取所有数据")
    //获取所有数据  get
    @ApiOperation(value = "查询全部数据")
    @GetMapping// http://localhost:8080/dept
    public List<Detail> queryAll() {
        return detailService.queryAll();
    }

    @MyLog(value = "分页 + 高级查询")
    //分页 + 高级查询  post
    @ApiOperation(value = "高级查询+分页功能")
    @PostMapping// http://localhost:8080/dept
    public PageList<Detail> queryPage(@RequestBody DepartmentQuery query) {
        return detailService.queryPage(query);
    }

    @GetMapping("/tree")
    public List<Detail> queryTree() {
        return detailService.queryTree();
    }


}
